package com.wxy.ideaplugin.utils;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.util.List;


public class XmlUtils {
    static String filePath = "/Users/wxy2020/workspace/lowcode/lowcode-ui/src/views/coding/flow/oa2/chatai/chatai/list.vue";

    public static void main(String[] args) throws Exception {
       /* Item item = new Item("123", "Book", 19.99);
        XmlMapper xmlMapper = new XmlMapper();
        String xmlString = xmlMapper.writeValueAsString(item);
        System.out.println(xmlString);*/
        /*XmlMapper xmlMapper = new XmlMapper();
        Item item = xmlMapper.readValue(new File(filePath), Item.class);
        System.out.println(item.template);*/

        /*DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(filePath);
        NodeList nodeList = document.getElementsByTagName("template");
        System.out.println(nodeList.getLength());*/

        String vueText = readFromInputStream();
        Document doc = Jsoup.parse(vueText);
        doc.select("#rowOptBtn").before("<aaaaaa/>");
        System.out.println(doc.html());

        //List<Element> elementList = doc.select("template el-table").first().select("el-table-column");
        //System.out.println(elementList.get(0).html());

        //System.out.println(headingsDiv.html());
        //获取<tbody>标签下的<td>标签
        //Elements headings = doc.select("tbody").select("td");
        //循环获取的<td>标签
        /*for (int i = 0; i < headings.size(); i++) {
            //获取<td>标签内的值
            String text = headings.get(i).text();
            //因为包含两个不同的<td>标签，所以需要取余
            if (i%2 == 0){
                //获取<td>标签下的<a>标签的 href 属性的值
                String href = headings.get(i).select("a").attr("href");
                System.out.println("文件下载地址："+href);
                System.out.println("文件名称："+text);
            }else {
                System.out.println("文件时间："+text);
            }
        }*/
    }
    /*static class Item {
        public String template;
        public String script;
        public String style;

    }*/
    public static String readFromInputStream()
            throws IOException {
        FileInputStream fileInputStream = new FileInputStream(filePath);
        StringBuilder resultStringBuilder = new StringBuilder();
        try (BufferedReader br = new BufferedReader(new InputStreamReader(fileInputStream))) {
            String line;
            while ((line = br.readLine()) != null) {
                resultStringBuilder.append(line).append("\n");
            }
        }
        return resultStringBuilder.toString();
    }
}
